package com.chunxuan.langquan.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.dao.bean.SubjectBean;
import com.chunxuan.langquan.support.util.Logg;
import com.chunxuan.langquan.ui.activity.SubjectActivity;
import com.google.gson.Gson;

import java.util.List;

/**
 * 课程adapter
 */
public class CourseAdapter extends RecyclerView.Adapter {
    private SparseArray<String> titles = new SparseArray<>();
    private Context mContext;
    private List<SubjectBean> subjectBeanList;
    public CourseAdapter(Context context, List<SubjectBean> subjectBeanList) {
        this.mContext=context;
        this.subjectBeanList=subjectBeanList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(isTitle(viewType)){
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_rlv_fragment1_title, parent, false);
            TitleViewHolder titleViewHolder = new TitleViewHolder(v);
            return titleViewHolder;
        }else {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_rlv_fragment1_course, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(v);
            return myViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(isTitle(position)){
            TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
            if(titles.get(position).contains("高中")){
                titleViewHolder.tvNum.setText("01");
            }else if(titles.get(position).contains("初中")){
                titleViewHolder.tvNum.setText("02");
            }else if(titles.get(position).contains("小学")){
                titleViewHolder.tvNum.setText("03");
            }
//            for(int i=0;i<titles.size();i++){
//                int num=i+1;
//                titleViewHolder.tvNum.setText("0"+num);
//            }
            titleViewHolder.tvCourseName.setText(titles.get(position));
            return;
        }
        for (int i = 0; i < titles.size(); i++){
            int key = titles.keyAt(i);
            if(position > titles.keyAt(titles.size() -1)){  //item在最后一个title之后的情况
                position -= titles.size();
                break;
            }else if(key < position && position < titles.keyAt(i + 1)){ //item在某两个title之间的情况
                position -= (i + 1);
                break;
            }
        }
        final int s = position;
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        Gson gson=new Gson();
        String kecheng=gson.toJson(subjectBeanList);
//        Logg.e("==课程66666---==="+kecheng);
        myViewHolder.tvCourseName.setText(subjectBeanList.get(s).getName());
        if(subjectBeanList.get(s).getName().contains("语文")||subjectBeanList.get(s).getName().contains("数学")
            ||subjectBeanList.get(s).getName().contains("英语")){
        }else{
            myViewHolder.tvCourseName.setTextColor(mContext.getResources().getColor(R.color.tv_course_color));
        }
        GridLayoutManager.LayoutParams lp= (GridLayoutManager.LayoutParams) myViewHolder.myItemView.getLayoutParams();
        if(s%3==0){
            lp.leftMargin=32;
            lp.rightMargin=13;
        }else if(s%3==1){
            lp.leftMargin=13;
            lp.rightMargin=13;
        }else if(s%3==2){
            lp.leftMargin=13;
            lp.rightMargin=32;
        }
        lp.topMargin=22;
        myViewHolder.myItemView.setLayoutParams(lp);
        myViewHolder.myItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(mContext, "第 " + (s + 1) + "  个item is clicked", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(mContext, SubjectActivity.class);
                intent.putExtra("subject_category_id",subjectBeanList.get(s).getSubject_category_id());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(subjectBeanList==null||titles==null){
            return 0;
        }else{
            return subjectBeanList.size()+titles.size();
        }
    }
    public void addTitle(int position, String title){
        titles.put(position, title);
    }

    @Override
    public int getItemViewType(int position) {
        if(isTitle(position)) {
            return position;  //返回title的位置，仅用作onCreateViewHolder方法中的判断--是title
        }else{
            return position + 100000; //返回title+100000的位置，仅用作onCreateViewHolder方法中的判断--不是title
        }
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        //如果是title就占据设置的spanCount个单元格
        final GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
        //Sets the source to get the number of spans occupied by each item in the adapter.
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(isTitle(position)){
                    return layoutManager.getSpanCount();
                }
                return 1;
            }
        });
    }

    private boolean isTitle(int position){
        return titles.get(position) == null ? false : true;
    }

    public static class TitleViewHolder extends RecyclerView.ViewHolder{
        TextView tvNum;
        TextView tvCourseName;

        public TitleViewHolder(View itemView) {
            super(itemView);
            tvNum = itemView.findViewById(R.id.tv_num);
            tvCourseName=itemView.findViewById(R.id.tv_grade_name);
        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvCourseName;
        View myItemView;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvCourseName = itemView.findViewById(R.id.tv_fragment_course_name);
            myItemView = itemView;
        }
    }
}
